<?php
/**
 * 
 * 2013-10-16 UTF-8
 * @author Rex Lee
 *
 */
class VoteAction extends Action {
	public $username=null;
	public $voteModel=null;
	public $videoModel=null;
	
	function _initialize() {
		import('@.Rex.autoLogin');
		$this->username=autoLogin::PC();
		$this->voteModel=M("vote","vv_","DB_CONFIG1");
		$this->videoModel=M('video', 'vv_', 'DB_CONFIG1');
		if (!$this->username) {//用户未登录
			$this->ajaxReturn(array('result'=>false, 'msg'=>'请登录后再投票！'));
		}
		$this->assign('us',$this->username);
	}
	function index() {
		$vid=(int)$_POST['vid'];
		if ((int)$vid<=0) {
			$this->ajaxReturn(array('result'=>false, 'msg'=>'非法操作！！！'));
		}
		$record=$this->voteModel->where(array('username'=>$this->username, 'vid'=>$vid))->find();
		if ($record) {//如果已经投票
			$this->ajaxReturn(array('result'=>false, 'msg'=>'用户已经过投票！'));
		}else {
			$votedata['ip']=get_client_ip();
			$votedata['username']=$this->username;
			$votedata['vid']=$vid;
			$this->voteModel->add($votedata);
			$count=$this->voteModel->query('select count(*) as c from vv_vote where vid='.$vid);
			$videodata['votes']=(int)$count[0]['c'];
			$rst=$this->videoModel->where(array('id'=>$vid))->data($videodata)->save();
			$this->ajaxReturn(array('result'=>true, 'msg'=>'您为'.$vid.'号作品投票成功，谢谢参与！', 'c'=>$videodata['votes']));
		}
	}
}